package com.lun.swordtowardoffer2.c07;

import java.util.LinkedList;
import java.util.Queue;

public class MovingAverage {
	private Queue<Integer> nums;
	private int capacity;
	private int sum;
	
	public MovingAverage (int size) {
		if(size <= 0)
			throw new IllegalArgumentException();
		nums = new LinkedList<>();
		capacity = size;
	}
	
    public double next (int val) {
    	if(capacity == nums.size()) {
    		sum -= nums.poll();
    	}
    	sum += val;
    	nums.offer(val);
    	
    	return sum * 1.0 / nums.size();
    }
}
